/**
 * Window.as
 * @author Pelephone
 * 窗体类,可以拖放的sprite
 */
 
package sparrowGui
{
	import sparrowGui.vars.ComponentVars;
	import sparrowGui.vars.WindowVars;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	
	import sparrowGui.utils.PubFun;

	public class WindowBak extends Component
	{
		public static const EVT_END_DRAG:String		= "evt_end_drag";
		public static const EVT_MINI_CLICK:String		= "evt_mini_click";
		public static const EVT_CLOSE_CLICK:String	= "evt_close_click";
		
		private var _title:TextField = new TextField();
		protected var titPan:Component;
		protected var closeBtn:DisplayObject;
		protected var miniBtn:DisplayObject;
		protected var maxBtn:DisplayObject;
		protected var bodyDisp:DisplayObject;
		
		private var btnDis:int = 2;
		private var textTag:String = "title";
		private var inDraging:Boolean = false;
		private var isMini:Boolean = false;
		
		/**
		 * 构造
		 */		
		public function WindowBak(tit:String="windos",sty:WindowVars=null)
		{
//			if(!sty)  sty = SparrowGUI.getInstance().windowCss;
			
			titPan	= sty.topPan;
			maxBtn	= sty.maxBtn;
			miniBtn	= sty.miniBtn;
			closeBtn = sty.closeBtn;
			
			textTag	= sty.textTag;
			btnDis	= sty.btnDis;
			
			super(sty);
			
//			_title.styleSheet = SparrowGUI.getInstance().txtCss;
			title = tit;
		}
		
		/**
		 * Initializes the component.
		 */
		override protected function init():void
		{
			draw();
			if(titPan)
				titPan.addEventListener(MouseEvent.MOUSE_DOWN, onMouseGoDown);
			
			if(style.bgImg)
				style.bgImg.addEventListener(MouseEvent.MOUSE_DOWN, onMouseGoDown);
			
			if(miniBtn){
				miniBtn.addEventListener(MouseEvent.CLICK,miniClick);
				maxBtn.addEventListener(MouseEvent.CLICK,miniClick);
			}
			
			if(closeBtn)
				closeBtn.addEventListener(MouseEvent.CLICK,closeClick);

			SparrowGUI.mainDisp.addEventListener(Event.MOUSE_LEAVE,mouseUp);
		}
		
		
		///////////////////////////////////
		// event handlers
		///////////////////////////////////
		
		// 鼠标按下拖动
		protected function onMouseGoDown(event:MouseEvent):void
		{
			inDraging = true;
			var dist:int = 20;
			// 边框 border
			var border:Rectangle = new Rectangle((dist-width), (dist-height),
				(SparrowGUI.stageWidth+width-dist*2),
				(SparrowGUI.stageHeight+height-dist*2));		// 允许的拖动范围(舞台)
			this.startDrag(false, border);
			PubFun.moveFront(this);
			this.stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp, false, 0, true);
		}
		
		// 停止拖动
		private function mouseUp(event:Object=null):void{
			if(!inDraging ) return;
			inDraging = false;
			this.stopDrag();
			style.bgImg.removeEventListener(MouseEvent.MOUSE_UP, mouseUp);
			this.dispatchEvent(new Event(EVT_END_DRAG));
		}
		
		// 鼠标按了最小化按钮
		protected function miniClick(event:MouseEvent):void
		{
			isMini = !isMini;
			
			if(isMini){
				graphics.clear();
				style.bgImg.scaleY	= 0;
				style.bgImg.visible	= false;
				miniBtn.visible = false;
				maxBtn.visible	= true;
			}else{
				style.bgImg.scaleY	= 1;
				style.bgImg.visible	= true;
				miniBtn.visible = true;
				maxBtn.visible	= false;
			}
			
			PubFun.moveFront(this);
			this.dispatchEvent(new Event(EVT_MINI_CLICK));
		}
		
		// 鼠标按了最小化按钮
		protected function closeClick(event:MouseEvent):void
		{
			dispose();
			this.dispatchEvent(new Event(EVT_CLOSE_CLICK));
		}
		
		
		///////////////////////////////////
		// getter/setters
		///////////////////////////////////
		public function set title(value:String):void
		{
			if(!textTag)
				_title.text = value;
			else
				_title.htmlText = PubFun.setTextTag(value,textTag);
		}
		
		public function get title():String
		{
			if(_title) return _title.text;
			else return "";
		}
		
		override public function dispose():void
		{
			SparrowGUI.mainDisp.removeEventListener(Event.MOUSE_LEAVE,mouseUp);
			titPan.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseGoDown);
			style.bgImg.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseGoDown);
			miniBtn.removeEventListener(MouseEvent.CLICK,miniClick);
			maxBtn.removeEventListener(MouseEvent.CLICK,miniClick);
			closeBtn.removeEventListener(MouseEvent.CLICK,closeClick);
			
			if(_title && _title.parent) _title.parent.removeChild(_title);
			_title = null;
			if(titPan && titPan.parent) titPan.parent.removeChild(titPan);
			titPan = null;
			if(closeBtn && closeBtn.parent) closeBtn.parent.removeChild(closeBtn);
			closeBtn = null;
			if(miniBtn && miniBtn.parent) miniBtn.parent.removeChild(miniBtn);
			miniBtn = null;
			if(maxBtn && maxBtn.parent) maxBtn.parent.removeChild(maxBtn);
			maxBtn =null;
			if(bodyDisp && bodyDisp.parent) bodyDisp.parent.removeChild(bodyDisp);
			bodyDisp = null;
			super.dispose();
		}
	}
}